package com.stx.day0921.aiAssistantManagement;
import java.util.*;

public class AI_Assistant_Management {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        ArrayList<aiType> list = new ArrayList<>();
        while (true) {
            ToDesk();
            System.out.print("请选择操作:");
            int choose = sc.nextInt();
            if (choose < 0 && choose > 9) {
                break;
            }
            if (choose == 1) {
                printAI(list);
            } else if (choose == 2) {
                System.out.print("请输入要查询的AI助手ID: ");
                String num = sc.next();
                System.out.println(findAi(list, num));
            } else if (choose == 3) {
                System.out.println("添加新AI助手");
                System.out.print("请输入添加AI助手的个数: ");
                int num = sc.nextInt();
                addAI(list, num);

            } else if (choose == 4) {
                System.out.println("请输入AI助手的ID:");
                String id = sc.next();
                modifyAI(list, id);
            } else if (choose == 5) {
                System.out.print("请输入删除AI助手的ID: ");
                String id = sc.next();
                delAI(list, id);
            } else if (choose == 6) {
                System.out.print("请输入需要启动的AI助手ID: ");
                String id = sc.next();
                System.out.println(startAI(list, id));
            } else if (choose == 7) {
                System.out.print("请输入需要停止的AI助手ID: ");
                String id = sc.next();
                System.out.println(stopAI(list, id));
            } else if (choose == 8) {
                System.out.println("请输入目标AI助手的ID: ");
                String id = sc.next();
                TaskExecuteAI(list, id);
            } else if (choose == 9) {
                statisticalInformation(list);
            } else if (choose == 0) {
                break;
            } else {
                System.out.println("==========没有该选项！=============");
            }
        }

    }

    //桌面
    public static void ToDesk() {
        System.out.println("========== AI助手管理器 ==========");
        System.out.println("1. 查看所有AI助手列表");
        System.out.println("2. 根据ID查询助手详情");
        System.out.println("3. 添加新的AI助手");
        System.out.println("4. 修改AI助手信息");
        System.out.println("5. 删除AI助手（按ID）");
        System.out.println("6. 启动AI助手服务");
        System.out.println("7. 停止AI助手服务");
        System.out.println("8. 为AI助手分配任务");
        System.out.println("9. 显示系统统计信息");
        System.out.println("0. 退出系统");
        System.out.println("================================");
    }

    //查看所有AI助手列表
    public static void printAI(ArrayList<aiType> list) {
        for (int i = 0; i < list.size(); i++) {
            aiType a = list.get(i);
            String str = a.show(list);
            System.out.println(str);
        }
    }

    //查找AI
    public static String findAi(ArrayList<aiType> list, String num) {
        for (int i = 0; i < list.size(); i++) {
            aiType a = list.get(i);
            if (a.getId().equals(num)) {
                String str = a.show(list);
                return "✅" + "查询结果：" + str;
            }
        }
        return "没有查询到你输入的AI助手ID！";
    }

    //添加AI
    public static void addAI(ArrayList<aiType> list, int num) {
        Scanner sc = new Scanner(System.in);
        Random r = new Random();
        for (int i = 0; i < num; i++) {

            System.out.println("请输入第"+(i+1)+"个ai助手的ID：");
            String id = sc.next();
            boolean flag = true;
            for (int j = 0; j < list.size(); j++) {
                if (id.equals(list.get(j).getId())){
                    flag = false;
                    break;
                }
            }
            if(flag){
                System.out.println("请输入第"+(i+1)+"个ai助手的名称：");
                String name = sc.next();
                System.out.println("请输入第"+(i+1)+"个ai助手的版本：");
                String version = sc.next();
                int cont = r.nextInt(5);
                String[] ty = {"语音助手", "数据分析助手", "图片生成助手",
                        "文本生成助手", "翻译助手",};
                String type = ty[cont];
                System.out.println("请输入第"+(i+1)+"个ai助手的状态：1.在线/0.离线");
                int n = sc.nextInt();
                String fg = "";
                if (n == 1) {
                    fg = "在线";
                } else if (n==0){
                    fg = "离线";
                }else {
                    System.out.println("输入有误！！！");
                    break;
                }
                System.out.println("请输入第"+(i+1)+"个ai助手的评分(0——100)：");
                int rating = sc.nextInt();
                if (rating<=100&&rating>=0){
                    aiType ai = new aiType(id, name, type, version, fg, rating);
                    list.add(ai);
                    System.out.println("✅ 助手【" + name + "】添加成功！");
                }else {
                    System.out.println("你输入的评分范围有误！");
                    break;
                }

            }else {
                System.out.println("你输如的编号已存在，请重新输入！");
            }

        }


    }

    //删除AI助手
    public static void delAI(ArrayList<aiType> list, String id) {
        for (int i = 0; i < list.size(); i++) {

            aiType a = list.get(i);
            if (a.getId().equals(id)) {
                list.remove(i);
                System.out.println("✅ 助手【" + a.getName() + "】已删除！");
                return;
            }
        }
    }


    //启动AI助手
    public static String startAI(ArrayList<aiType> list, String id) {
        for (int i = 0; i < list.size(); i++) {
            aiType a = list.get(i);
            if (a.getId().equals(id)) {
                if (a.getFlag().equals("在线")) {
                    String str = "AI助手【" + a.getName() + "】在线，无需再次启动";
                    return str;
                } else {
                    a.setFlag("\uD83D\uDFE2在线");
                    String str = "AI助手【" + a.getName() + "】启动成功";
                    return str;
                }
            }
        }
        return "没有查询到你输入的AI助手ID！";
    }

    //停止AI助手
    public static String stopAI(ArrayList<aiType> list, String id) {
        for (int i = 0; i < list.size(); i++) {
            aiType a = list.get(i);
            if (a.getId().equals(id)) {
                if (a.getFlag().equals("在线")) {
                    a.setFlag("离线");
                    String str = "AI助手【" + a.getName() + "】已停止";
                    return str;
                } else {
                    String str = "AI助手【" + a.getName() + "】是离线状态";
                    return str;
                }
            }
        }
        return "没有查询到你输入的AI助手ID！";
    }

    //修改信息
    public static void modifyAI(ArrayList<aiType> list, String id) {
        Scanner sc = new Scanner(System.in);
        for (int i = 0; i < list.size(); i++) {
            aiType a = list.get(i);
            if (a.getId().equals(id)) {
                int fg;
                while (true) {

                    do {
                        System.out.println("请输入要修改的属性：0.id 1.name 2.version 3.type 4.flag 5.rating");
                        int num = sc.nextInt();
                        if (num == 0) {
                            System.out.println("请输入要修改id的值：");
                            String d = sc.next();
                            a.setId(d);
                            System.out.println("✅ 修改成功！");

                        } else if (num == 1) {
                            System.out.println("请输入要修改name的值：");
                            String name = sc.next();
                            a.setName(name);
                            System.out.println("✅ 修改成功！");

                        } else if (num == 2) {
                            System.out.println("请输入要修改version的值：");
                            String version = sc.next();
                            a.setVersion(version);
                            System.out.println("✅ 修改成功！");

                        } else if (num == 3) {
                            System.out.println("请输入要修改type的值：");
                            String type = sc.next();
                            a.setType(type);
                            System.out.println("✅ 修改成功！");

                        } else if (num == 4) {
                            System.out.println("请输入要修改flag的值：1.在线/0.离线");
                            int n = sc.nextInt();
                            String flg = "";
                            if (n == 1) {
                                flg = "在线";
                            } else if (n==0){
                                flg = "离线";
                            }else {
                                System.out.println("输入有误！！！");
                                break;
                            }
                            a.setFlag(flg);
                            System.out.println("✅ 修改成功！");

                        } else if (num == 5) {
                            System.out.println("请输入要修改rating的值(0——100)：");
                            int rating = sc.nextInt();
                            if (rating<=100&&rating>=0){
                                a.setRating(rating);
                                System.out.println("✅ 修改成功！");
                            }else {
                                System.out.println("你输入的评分范围有误！");
                                break;
                            }
                        }
                        System.out.println("是否继续修改 1.继续 2.退出");
                        fg = sc.nextInt();
                    } while (fg == 1);
                    break;
                }
            }
        }
    }

    //为AI助手分配任务"
    public static void TaskExecuteAI(ArrayList<aiType> list, String id) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入任务内容：");
        String str = sc.next();
        for (int i = 0; i < list.size(); i++) {
            aiType a = list.get(i);
            if (a.getId().equals(id)) {
                if (a.getFlag().equals("离线")) {
                    System.out.println("❌" + a.getName() + "当前离线，无法执行任务。");
                } else {
                    System.out.println(a.getName() + "(" + a.getType() + ") 正在处理任务：" + str);
                }
            }
        }

    }

    //系统统计信息
    public static void statisticalInformation(ArrayList<aiType> list) {
        int online = 0;
        int offline = 0;
        double sum = 0;
        for (int i = 0; i < list.size(); i++) {
            aiType a = list.get(i);
            if (a.getFlag().equals("在线")) {
                sum += a.getRating();
                online++;
            } else if (a.getFlag().equals("离线")) {
                offline++;
                sum += a.getRating();
            }
        }
        System.out.println("系统统计信息：");
        System.out.println("总助手数：" + list.size());
        System.out.println("在线助手数：" + online);
        System.out.println("离线助手数：" + offline);
        System.out.println("平均能力评分：" + (double) (sum / list.size()));

    }

}
